package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.domain.BaseArea;
import com.ruoyi.system.domain.BaseCity;
import com.ruoyi.system.domain.BaseProvince;
import com.ruoyi.system.domain.SysRegion;
import com.ruoyi.system.domain.vo.InitializationAreaVo;
import com.ruoyi.system.domain.vo.InitializationCityVo;
import com.ruoyi.system.domain.vo.InitializationVo;
import com.ruoyi.system.mapper.BaseProvinceMapper;
import com.ruoyi.system.service.IBaseAreaService;
import com.ruoyi.system.service.IBaseCityService;
import com.ruoyi.system.service.IBaseProvinceService;
import com.ruoyi.system.service.ISysRegionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 *
 *
 * @author yangsen
 * @date 2023-05-10
 */
@Service
@Slf4j
public class BaseProvinceServiceImpl extends ServiceImpl<BaseProvinceMapper, BaseProvince> implements IBaseProvinceService {

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

//    @Autowired
//    private IBaseAreaService baseAreaService;
//
//    @Autowired
//    private IBaseCityService iBaseCityService;

    @Autowired
    private ISysRegionService sysRegionService;



    private static String redisConfig = "initializationProvince";
    private static String baseAreaConfig = "baseArea";
    private static String baseCityConfig = "baseCity";
    private static String baseProvinceConfig = "baseProvince";

    /**
     * 初始化地区信息
     */
    @Override
    @PostConstruct
    public void initializationProvince() {
        log.info("开始初始化地区信息");
//
//        List<SysRegion> province = sysRegionService.searchList(1);
//        List<SysRegion> city = sysRegionService.searchList(2);
//        List<SysRegion> county = sysRegionService.searchList(3);
//
//
//        Map<Long, SysRegion> provinceMap = province.stream().collect(Collectors.toMap(SysRegion::getCode, Function.identity()));
//        Map<Long, SysRegion> cityMap = city.stream().collect(Collectors.toMap(SysRegion::getCode, Function.identity()));
//        Map<Long, SysRegion> countyMap = county.stream().collect(Collectors.toMap(SysRegion::getCode, Function.identity()));
//
//        redisTemplate.opsForValue().set(baseAreaConfig,countyMap);
//        redisTemplate.opsForValue().set(baseCityConfig,cityMap);
//        redisTemplate.opsForValue().set(baseProvinceConfig,provinceMap);
//
//        Map<String, List<InitializationAreaVo>> baseArealistMap = countyMap.stream()
//                .map(res -> new InitializationAreaVo(res.getCode(), res.getName(), res.getCityCode()))
//                .collect(Collectors.groupingBy(InitializationAreaVo::getCityCode));
//
//        List<InitializationCityVo> initializationCityVos = cityMap.stream()
//                .map(res -> new InitializationCityVo(res.getCode(), res.getName(), res.getProvinceCode()))
//                .collect(Collectors.toList());
//
//        List<InitializationVo> initializationVoList = baseProvinces.stream().map(res -> new InitializationVo(res.getCode(), res.getName()))
//                .collect(Collectors.toList());
//
//        for (InitializationCityVo initializationCityVo : initializationCityVos) {
//            List<InitializationAreaVo> initializationAreaVoList = baseArealistMap.get(initializationCityVo.getCode());
//            initializationCityVo.setChildren(initializationAreaVoList);
//        }
//
//        Map<String, List<InitializationCityVo>> baseCitylistMap = initializationCityVos.stream().collect(Collectors.groupingBy(InitializationCityVo::getProvinceCode));
//        for (InitializationVo initializationVo : initializationVoList) {
//            initializationVo.setChildren(baseCitylistMap.get(initializationVo.getCode()));
//        }
//        redisTemplate.opsForValue().set(redisConfig,initializationVoList);

        List<SysRegion> province = sysRegionService.searchList(1);
        List<SysRegion> city = sysRegionService.searchList(2);
        List<SysRegion> county = sysRegionService.searchList(3);

        Map<Long, SysRegion> provinceMap = province.stream().collect(Collectors.toMap(SysRegion::getCode, Function.identity()));
        Map<Long, SysRegion> cityMap = city.stream().collect(Collectors.toMap(SysRegion::getCode, Function.identity()));
        Map<Long, SysRegion> countyMap = county.stream().collect(Collectors.toMap(SysRegion::getCode, Function.identity()));

        redisTemplate.opsForValue().set(baseAreaConfig, countyMap);
        redisTemplate.opsForValue().set(baseCityConfig, cityMap);
        redisTemplate.opsForValue().set(baseProvinceConfig, provinceMap);

        Map<String, List<InitializationAreaVo>> baseArealistMap = county.stream()
                .map(res -> new InitializationAreaVo(res.getCode(), res.getName(), String.valueOf(res.getCode())))
                .collect(Collectors.groupingBy(InitializationAreaVo::getCityCode));

        List<InitializationCityVo> initializationCityVos = city.stream()
                .map(res -> new InitializationCityVo(res.getCode(), res.getName(), String.valueOf(res.getCode())))
                .collect(Collectors.toList());

        List<InitializationVo> initializationVoList = province.stream()
                .map(res -> new InitializationVo(res.getCode(), res.getName()))
                .collect(Collectors.toList());

        for (InitializationCityVo initializationCityVo : initializationCityVos) {
            List<InitializationAreaVo> initializationAreaVoList = baseArealistMap.get(initializationCityVo.getCode());
            initializationCityVo.setChildren(initializationAreaVoList);
        }

        Map<String, List<InitializationCityVo>> baseCitylistMap = initializationCityVos.stream()
                .collect(Collectors.groupingBy(InitializationCityVo::getProvinceCode));

        for (InitializationVo initializationVo : initializationVoList) {
            initializationVo.setChildren(baseCitylistMap.get(initializationVo.getCode()));
        }

        redisTemplate.opsForValue().set(redisConfig, initializationVoList);



        log.info("初始化地区信息成功");
    }
}
